package com.wellsoft.mobileoa.utils.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.BaseWidget;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.utils.view.FormView;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageField;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageFieldButton;
import com.wellsoft.mobileoa.wsmodel.WSOA_PostField;
import com.wmobile.utils.ServiceResponse;

public class TextareaWidget extends BaseWidget {
	private EditText fieldValueEt;
	private TextView fieldTitleTv;
	private LinearLayout selectLl;
	private FormView formView;

	public TextareaWidget(WSOA_PageField field, BaseView baseView) {
		this.field = field;
		this.baseView = baseView;
	}

	@Override
	public View getView() {
		view = LayoutInflater.from(baseView.getBaseActivity()).inflate(
				R.layout.textareawidget, null);
		fieldValueEt = (EditText) view
				.findViewById(R.id.textareawidget_fieldvalueet);
		fieldValueEt.addTextChangedListener(onTextChanged);
		fieldTitleTv = (TextView) view
				.findViewById(R.id.textareawidget_fieldtitletv);
		selectLl = (LinearLayout) view
				.findViewById(R.id.textareawidget_selectll);
		selectLl.setOnClickListener(onSelectListener);

		if (field.getFieldButtons() != null
				&& field.getFieldButtons().size() > 0) {
			WSOA_PageFieldButton button = field.getFieldButtons().get(0);
			if (!SystemConfig.stringIsNullOrEmpty(button.getMessage())) {
				fieldTitleTv.setText(button.getMessage());
			} else {
				fieldTitleTv.setText(field.getFieldTitle());
			}
		} else {
			fieldTitleTv.setText(field.getFieldTitle());
			selectLl.setVisibility(View.GONE);
		}
		fieldValueEt.setText(field.getFieldValue());

		if (!SystemConfig.stringIsNullOrEmpty(field.getFieldRead())
				&& field.getFieldRead().equals("1")) {
			fieldValueEt.setFocusable(false);
		}
		if (SystemConfig.isInt(field.getRowCount())) {
			fieldValueEt.getLayoutParams().height = SystemConfig.dip2px(
					baseView.getBaseActivity(),
					32 * Integer.parseInt(field.getRowCount()));
			// fieldValueEt.setHeight(SystemConfig.dip2px(
			// baseView.getBaseActivity(),
			// 24 * Integer.parseInt(field.getRowCount())));
		}

		formView = (FormView) baseView;
		fieldValueEt.setOnTouchListener(onTouchListener);
		baseView.setHasEdited(false);
		return view;
	}

	OnTouchListener onTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN
					|| event.getAction() == MotionEvent.ACTION_MOVE) {
				formView.scrollView.requestDisallowInterceptTouchEvent(true);
			} else {
				formView.scrollView.requestDisallowInterceptTouchEvent(false);
			}
			return false;
		}

	};

	// OnFocusChangeListener focusChangeListener = new OnFocusChangeListener() {
	//
	// @Override
	// public void onFocusChange(View v, boolean hasFocus) {
	// if(hasFocus){v.getParent().getParent().getParent().getParent()
	// // FormView formView = (FormView)baseView;
	// // formView.scrollView.requestDisallowInterceptTouchEvent(true);
	// }else{
	// FormView formView = (FormView)baseView;
	// formView.scrollView.requestDisallowInterceptTouchEvent(false);
	// }
	//
	// }
	// };

	@Override
	public WSOA_PostField getValue() {
		List<String> listValue = new ArrayList<String>();
		WSOA_PostField postfield = new WSOA_PostField();
		postfield.setFieldName(field.getFieldName());

		if (fieldValueEt.getText() != null
				&& !SystemConfig.stringIsNullOrEmpty(fieldValueEt.getText()
						.toString())) {
			if (!SystemConfig.stringIsNullOrEmpty(field.getSeparator())) {
				String tmpSeparator = field.getSeparator();
				if (tmpSeparator.toUpperCase().equals("LINE")) {
					tmpSeparator = "\n";
				}
				String[] fieldValues = fieldValueEt.getText().toString()
						.split(tmpSeparator);
				for (int i = 0; i < fieldValues.length; i++) {
					listValue.add(fieldValues[i]);
				}
			} else {
				listValue.add(fieldValueEt.getText().toString());
			}
		}
		postfield.setFieldValues(listValue);

		return postfield;
	}

	@Override
	public String getRawValue() {
		return fieldValueEt.getText().toString();
	}

	@Override
	public void setValue(Object widgetValue) {
		fieldValueEt.setText(widgetValue.toString());
		baseView.setHasEdited(true);
		setHasEdited(true);
		setHasValided(false);
	}

	@Override
	public void resetWidget() {
		fieldValueEt.setText("");
	}

	@Override
	public ServiceResponse checkValid() {
		String fieldValue = fieldValueEt.getText().toString();
		if (field.getRequired() != null) {
			if (field.getRequired().equals("1")
					&& SystemConfig.stringIsNullOrEmpty(fieldValue)) {
				return new ServiceResponse(false, fieldTitleTv.getText()
						+ "不能为空！");
			} else if (field.getRequired().equals("number")) {
				if (!(SystemConfig.stringIsNullOrEmpty(fieldValue)
						|| SystemConfig.isInt(fieldValue) || SystemConfig
							.isNumberic(fieldValue))) {
					return new ServiceResponse(false, field.getFieldTitle()
							+ "不能为空且必须为数字！");
				}
			}
		}
		return new ServiceResponse(true);
	}

	public OnClickListener onSelectListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			AlertDialog alertDialog = baseView.getBaseActivity()
					.showWaitingDialog();
			WSOA_PageFieldButton button = field.getFieldButtons().get(0);
			ServiceResponse response = BaseAction.executeActions(
					button.getActionID(), button.getAction(), baseView);
			alertDialog.dismiss();
			if (!response.getExeFlag()) {
				baseView.getBaseActivity().showMessage(response.getMessage());
				return;
			}
		}
	};

	public TextWatcher onTextChanged = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			baseView.setHasEdited(true);
			setHasEdited(true);
			setHasValided(false);
		}
	};

}
